// 血条管理类
// 实现类似拳皇游戏的血条过渡效果

import { TimeManager } from './TimeManager.js';

export class HealthBarManager {
    constructor(maxHealth = 5) {
        this.maxHealth = maxHealth;
        this.currentHealth = maxHealth;
        this.displayHealth = maxHealth; // 用于过渡动画的血量
        this.transitionHealth = maxHealth; // 过渡血条显示的血量
        
        // DOM元素引用
        this.healthBarCurrent = null;
        this.healthBarTransition = null;
        this.healthCurrentText = null;
        this.healthMaxText = null;
        
        // 过渡动画控制
        this.isTransitioning = false;
        this.transitionStartTime = 0;
        this.transitionDuration = 1200; // 过渡动画持续时间（毫秒）
        this.transitionDelay = 800; // 过渡动画开始前的延迟（毫秒）
        
        this.initializeElements();
    }
    
    initializeElements() {
        // 获取DOM元素引用
        this.healthBarCurrent = document.getElementById('health-bar-current');
        this.healthBarTransition = document.getElementById('health-bar-transition');
        this.healthCurrentText = document.getElementById('health-current');
        this.healthMaxText = document.getElementById('health-max');
        
        // 初始化显示
        this.updateDisplay();
    }
    
    // 设置最大血量
    setMaxHealth(maxHealth) {
        this.maxHealth = maxHealth;
        this.currentHealth = Math.min(this.currentHealth, maxHealth);
        this.displayHealth = this.currentHealth;
        this.transitionHealth = this.currentHealth;
        this.updateDisplay();
    }
    
    // 设置当前血量（立即更新）
    setCurrentHealth(health) {
        const newHealth = Math.max(0, Math.min(this.maxHealth, health));
        
        if (newHealth !== this.currentHealth) {
            const oldHealth = this.currentHealth;
            this.currentHealth = newHealth;
            
            // 如果是减血，启动过渡动画
            if (newHealth < oldHealth) {
                this.startTransition();
            } else {
                // 如果是加血，立即更新所有值
                this.displayHealth = newHealth;
                this.transitionHealth = newHealth;
                this.isTransitioning = false;
            }
            
            this.updateDisplay();
        }
    }
    
    // 启动过渡动画
    startTransition() {
        this.isTransitioning = true;
        this.transitionStartTime = Date.now();
        
        // 立即更新当前血条显示
        this.displayHealth = this.currentHealth;
        // 过渡血条暂时保持原来的值，稍后开始缓慢减少
    }
    
    // 更新过渡动画（在游戏循环中调用）
    update(deltaTime) {
        if (!this.isTransitioning) {
            return;
        }
        
        const currentTime = Date.now();
        const elapsedTime = currentTime - this.transitionStartTime;
        
        // 延迟开始过渡动画
        if (elapsedTime < this.transitionDelay) {
            return;
        }
        
        // 计算过渡进度
        const transitionProgress = Math.min(1, (elapsedTime - this.transitionDelay) / this.transitionDuration);
        
        // 使用缓出动画曲线
        const easeProgress = 1 - Math.pow(1 - transitionProgress, 3);
        
        // 计算过渡血条的当前值
        const startHealth = this.transitionHealth;
        const targetHealth = this.currentHealth;
        const newTransitionHealth = startHealth + (targetHealth - startHealth) * easeProgress;
        
        this.transitionHealth = newTransitionHealth;
        
        // 动画完成
        if (transitionProgress >= 1) {
            this.isTransitioning = false;
            this.transitionHealth = this.currentHealth;
        }
        
        this.updateDisplay();
    }
    
    // 更新UI显示
    updateDisplay() {
        if (!this.healthBarCurrent || !this.healthBarTransition) {
            return;
        }
        
        // 计算血量百分比
        const currentPercent = (this.displayHealth / this.maxHealth) * 100;
        const transitionPercent = (this.transitionHealth / this.maxHealth) * 100;
        
        // 更新血条宽度
        this.healthBarCurrent.style.width = `${currentPercent}%`;
        this.healthBarTransition.style.width = `${transitionPercent}%`;
        
        // 更新血条颜色样式
        this.updateHealthBarColor();
        
        // 更新文本显示
        if (this.healthCurrentText) {
            this.healthCurrentText.textContent = Math.ceil(this.currentHealth);
        }
        if (this.healthMaxText) {
            this.healthMaxText.textContent = this.maxHealth;
        }
    }
    
    // 更新血条颜色样式
    updateHealthBarColor() {
        if (!this.healthBarCurrent) return;
        
        const healthPercent = this.displayHealth / this.maxHealth;
        
        // 移除所有颜色类
        this.healthBarCurrent.classList.remove('health-high', 'health-medium', 'health-low');
        
        // 根据血量百分比添加对应的颜色类
        if (healthPercent > 0.6) {
            this.healthBarCurrent.classList.add('health-high');
        } else if (healthPercent > 0.3) {
            this.healthBarCurrent.classList.add('health-medium');
        } else if (healthPercent > 0) {
            this.healthBarCurrent.classList.add('health-low');
        }
    }
    
    // 减少血量
    takeDamage(damage = 1) {
        this.setCurrentHealth(this.currentHealth - damage);
    }
    
    // 恢复血量
    heal(amount = 1) {
        this.setCurrentHealth(this.currentHealth + amount);
    }
    
    // 检查是否死亡
    isDead() {
        return this.currentHealth <= 0;
    }
    
    // 获取当前血量
    getCurrentHealth() {
        return this.currentHealth;
    }
    
    // 获取最大血量
    getMaxHealth() {
        return this.maxHealth;
    }
    
    // 获取血量百分比
    getHealthPercent() {
        return this.currentHealth / this.maxHealth;
    }
    
    // 重置血量
    reset() {
        this.currentHealth = this.maxHealth;
        this.displayHealth = this.maxHealth;
        this.transitionHealth = this.maxHealth;
        this.isTransitioning = false;
        this.updateDisplay();
    }
}